import { AxiosRequestConfig } from './types'
import { AxiosInstance } from './types'
import Axios from './core/Axios'
import { extend } from './helpers/utils'
import defaults from './defaults'

function createInstance(config: AxiosRequestConfig): AxiosInstance {
  const context = new Axios(config)

  // instance 绑定了 request 方法
  // instance() === request()
  // 所以可以使用 axios() 来调用
  const instance = Axios.prototype.request.bind(context)
  extend(instance, context)
  return instance as AxiosInstance
}

const axios = createInstance(defaults)

export default axios
